home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
ctlpuz_1
/
ctlpuzzl.ctl
< prev
next >
Wrap
Text File
|
1998-05-29
|
9KB
|
331 lines
VERSION 5.00
Begin VB.UserControl ctlPuzzle
BackColor = &H00C0FFFF&
ClientHeight = 5130
ClientLeft = 0
ClientTop = 0
ClientWidth = 4125
ScaleHeight = 342
ScaleMode = 3 'Pixel
ScaleWidth = 275
Begin VB.Timer Timer1
Interval = 1000
Left = 1800
Top = 3240
End
Begin VB.CommandButton cmdShuffle
Caption = "cmdShuffle"
Height = 495
Left = 2400
TabIndex = 0
Top = 4320
Width = 1215
End
Begin VB.ComboBox cmbSize
Height = 315
Left = 600
TabIndex = 1
Text = "cmbSize"
Top = 4440
Width = 1215
End
Begin VB.CommandButton cmdButton
Caption = "cmdButton"
Height = 495
Index = 0
Left = 1440
TabIndex = 2
Top = 1920
Width = 1215
End
Begin VB.Label lblTime
BackStyle = 0 'Transparent
Caption = "lblTime"
Height = 255
Left = 1800
TabIndex = 3
Top = 120
Width = 1095
End
End
Attribute VB_Name = "ctlPuzzle"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'-------------------------------------------------------------------------
'Author: Anders Fransson
'Email: anders.fransson@home.se
'Internet: http://hem1.passagen.se/fylke
'Date: 97-12-09
'-------------------------------------------------------------------------
Option Explicit
Private m_bPuzzleSolved As Boolean
Private m_iEmptyIndex As Integer
Private m_iSize As Integer
Private m_lTime As Long
Private Const MIN_SIZE As Byte = 3
Private Const MAX_SIZE As Byte = 7
'Text constants
Private Const TEXT_SHUFFLE As String = "Shuffle"
Private Const TEXT_NEW_GAME As String = "New Game"
Private Const TEXT_TIME As String = "Time:"
Private Const TEXT_PUZZLE As String = "Puzzle"
Private Const TEXT_HIGH_SCORE As String = "High score"
Private Const TEXT_SIZE As String = "Size"
Private Const TEXT_TIME_S As String = "Time"
Private Const TEXT_PLAYER As String = "Player"
Private Const TEXT_INPUT_PLAYER As String = "Write your name!"
Private Const TEXT_ANDERS_GAMES As String = "Anders Franssons Made In Home Games"
Private Static Sub cmdButton_MouseDown(Index As Integer, Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Dim i%, xEmpty%, yEmpty%, xClicked%, yClicked%
'Calculate coordinates for buttons
xEmpty = (m_iEmptyIndex) Mod m_iSize
yEmpty = (m_iEmptyIndex) \ m_iSize
xClicked = (Index) Mod m_iSize
yClicked = (Index) \ m_iSize
'Change buttons if empty is near
If (xClicked = xEmpty + 1 And yClicked = yEmpty) Or _
(xClicked = xEmpty - 1 And yClicked = yEmpty) Or _
(yClicked = yEmpty + 1 And xClicked = xEmpty) Or _
(yClicked = yEmpty - 1 And xClicked = xEmpty) Then
ChangeButtons (Index)
PlaySound App.Path & "\Move.wav"
End If
'Check if puzzle's solved
For i = 0 To m_iSize ^ 2 - 2
If Val(cmdButton(i).Caption) = i + 1 Then
m_bPuzzleSolved = True
Else
m_bPuzzleSolved = False
Exit For
End If
Next i
If m_bPuzzleSolved Then
If Timer1.Enabled Then PlaySound App.Path & "\Applause.wav"
Timer1.Enabled = False
WriteHighScore
m_lTime = 0
cmdShuffle.Caption = TEXT_SHUFFLE
cmdShuffle.SetFocus
Else
cmdShuffle.Caption = TEXT_NEW_GAME
End If
End Sub
Private Sub cmdShuffle_Click()
If m_bPuzzleSolved Then
Shuffle
Else
NewGame
End If
PlaySound App.Path & "\Shuffle.wav"
End Sub
Private Sub cmbSize_Click()
If cmbSize.Text = "High Score" Then
CheckHighScore
Exit Sub
End If
If Not (m_iSize = cmbSize.Text) Then
m_iSize = cmbSize.Text
NewGame
End If
End Sub
Private Sub Timer1_Timer()
m_lTime = m_lTime + 1
lblTime.Caption = TEXT_TIME & " " & m_lTime & " s"
End Sub
Private Static Sub NewGame()
Dim i%, j%, iSide%
lblTime = ""
m_lTime = 0
Timer1.Enabled = False
m_bPuzzleSolved = True
iSide = Int((90 / m_iSize)) * 2 + 10
'Hide butons and set caption
For i = 0 To MAX_SIZE ^ 2 - 1
cmdButton(i).Visible = False
cmdButton(i).Caption = i + 1
Next i
'Place buttons
For i = 0 To m_iSize - 1
For j = 0 To m_iSize - 1
cmdButton(i * m_iSize + j).Height = iSide
cmdButton(i * m_iSize + j).Width = iSide
cmdButton(i * m_iSize + j).Left = iSide / 2 + iSide * j
cmdButton(i * m_iSize + j).Top = 10 + iSide / 2 + iSide * i
cmdButton(i * m_iSize + j).Visible = True
Next j
Next i
m_iEmptyIndex = m_iSize ^ 2 - 1
cmdButton(m_iEmptyIndex).Visible = False
cmdShuffle.Caption = TEXT_SHUFFLE
End Sub
Private Static Sub Shuffle()
Dim bMove As Boolean
Dim i%, xCoord%, yCoord%, iRand%
'Hide buttons before shuffle
For i = 0 To m_iSize ^ 2 - 1
cmdButton(i).Visible = False
Next i
'Coordinates for empty button
xCoord = (m_iEmptyIndex) Mod m_iSize
yCoord = (m_iEmptyIndex) \ m_iSize
'Move buttons in random directions
i = 0
While i < m_iSize ^ 4
bMove = False
iRand = Int(4 * Rnd)
If (iRand = 0) And (xCoord > 0) Then
xCoord = xCoord - 1
bMove = True
ElseIf (iRand = 1) And (xCoord < m_iSize - 1) Then
xCoord = xCoord + 1
bMove = True
ElseIf (iRand = 2) And (yCoord > 0) Then
yCoord = yCoord - 1
bMove = True
ElseIf (iRand = 3) And (yCoord < m_iSize - 1) Then
yCoord = yCoord + 1
bMove = True
End If
If bMove Then
cmdButton(m_iEmptyIndex).Caption = _
cmdButton(m_iSize * yCoord + xCoord).Caption
m_iEmptyIndex = m_iSize * yCoord + xCoord
i = i + 1
End If
Wend
For i = 0 To m_iSize ^ 2 - 1
cmdButton(i).Visible = True
Next i
cmdShuffle.Caption = TEXT_NEW_GAME
cmdButton(m_iEmptyIndex).Visible = False
m_bPuzzleSolved = False
Timer1.Enabled = True
End Sub
Private Sub ChangeButtons(Index As Integer)
'Change caption and visibility of clicked and empty button
cmdButton(m_iEmptyIndex).Caption = cmdButton(Index).Caption
cmdButton(m_iEmptyIndex).Visible = True
cmdButton(m_iEmptyIndex).SetFocus
m_iEmptyIndex = Index
cmdButton(Index).Visible = False
cmdButton(Index).Caption = ""
End Sub
Private Sub UserControl_Initialize()
Dim i%
'Initialize random number generator
Randomize
'Load buttons
For i = 1 To MAX_SIZE ^ 2 - 1
Load cmdButton(i)
Next i
'Add combo box items
For i = MIN_SIZE To MAX_SIZE
cmbSize.AddItem i
Next i
cmbSize.AddItem "High Score"
'Auto click in combo
cmbSize.ListIndex = 1
m_iSize = cmbSize.Text
End Sub
Private Static Sub CheckHighScore()
Dim strHighScore As String
Dim i%
strHighScore = TEXT_SIZE & Chr(9) & TEXT_TIME_S & Chr(9) & TEXT_PLAYER & _
Chr(10) & Chr(13) & Chr(13)
'Get high score from registry
For i = MIN_SIZE To MAX_SIZE
strHighScore = strHighScore & i & Chr(9) & _
GetSetting(TEXT_ANDERS_GAMES, TEXT_PUZZLE, i, "-") & Chr(9) & _
GetSetting(TEXT_ANDE